<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Hairsizeoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('hairsizeoption');
	}
	public function ajax_hairsizeoption_get(){
		$hairsizeoptionEntity =  new CI_Hair_size_option_entity();
		$hairsizeoption = $hairsizeoptionEntity->getHairSizeOptionByCode($_REQUEST['code']);
		echo json_encode($hairsizeoption);
	}
	public function ajax_hairsizeoption_list(){
		$hairsizeoptionEntity =  new CI_Hair_size_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$hairsizeoptionList = $hairsizeoptionEntity->getHairSizeOptionListByLanguage($defaultLanguageId);
		$data['hairsizeoptionList'] = $hairsizeoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/hairsizeoptions/index', $data);
	}
	public function ajax_hairsizeoption_save(){
		$hairsizeoptionNameList = $_REQUEST['hairsizeoption'];
		$hairsizeoptionNameDefault = '';
		foreach ($hairsizeoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$hairsizeoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($hairsizeoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_hairsizeoption_require');
		}else{
			$code = 0;
			foreach ($hairsizeoptionNameList as $key => $value) {
				$hairsizeoptionEntity =  new CI_Hair_size_option_entity();
				if($value != null && $value != ''){
					$hairsizeoptionEntity->setHairStyle($value);
				}else{
					$hairsizeoptionEntity->setHairStyle($hairsizeoptionNameDefault);
				}
				$hairsizeoptionEntity->setLanguageId($key);
				$hairsizeoptionEntity->setCode($code);
				$id = $hairsizeoptionEntity->save();
				if($code == 0){
					$code = $id;
					$hairsizeoptionEntity->setId($id);
					$hairsizeoptionEntity->setCode($code);
					$hairsizeoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_hairsizeoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_hairsizeoption_update(){
		$hairsizeoptionNameList = $_REQUEST['hairsizeoption'];
		$hairsizeoptionIdList = $_REQUEST['hairsizeoption_id'];
		$hairsizeoptionCode = $_REQUEST['hairsizeoption_code'];
		$hairsizeoptionNameDefault = '';
		foreach ($hairsizeoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$hairsizeoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($hairsizeoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_hairsizeoption_require');
		}else{
			foreach ($hairsizeoptionNameList as $key => $value) {
				$hairsizeoptionEntity =  new CI_Hair_size_option_entity();
				if($value != null && $value != ''){
					$hairsizeoptionEntity->setHairStyle($value);
				}else{
					$hairsizeoptionEntity->setHairStyle($hairsizeoptionNameDefault);
				}
				$hairsizeoptionEntity->setLanguageId($key);
				$hairsizeoptionEntity->setCode($hairsizeoptionCode);
				$hairsizeoptionEntity->setId($hairsizeoptionIdList[$key]);
				$hairsizeoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_hairsizeoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_hairsizeoption_delete(){
		$result = array();
		$hairsizeoptionEntity =  new CI_Hair_size_option_entity();
		$result['result'] = $hairsizeoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_hairsizeoption_delete_success');
		}else{
			$result['message'] = lang('escort_hairsizeoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>